package com.zmx.patients.view.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.wang.adapters.interfaceabstract.OnItemClickListener;
import com.zmx.patients.BaseActivity;
import com.zmx.patients.R;
import com.zmx.patients.adapter.MyRecipeAdapter;
import com.zmx.patients.okhttp.OkHttp3ClientManager;
import com.zmx.patients.pojo.MyRecipe;
import com.zmx.patients.utils.AllUtil;
import com.zmx.patients.view.util.MySharedPreferences;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MyPrescriptionActivity extends BaseActivity {

    private RecyclerView reView;
    private SmartRefreshLayout refreshLayout;
    private MyRecipeAdapter adapter;
    private List<MyRecipe> lists;
    private View mEmptyLayout;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_prescription;
    }

    @Override
    protected void initViews() {

        setToolbar(R.id.tool_bar);

        mEmptyLayout = findViewById(R.id.empty);
        reView = findViewById(R.id.reView);
        reView.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new MyRecipeAdapter(mActivity);
        reView.setAdapter(adapter);
        lists = new ArrayList<>();
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            protected void onItemClick(View view, int listPosition) {

                Bundle bundle = new Bundle();
                bundle.putString("temp_id",lists.get(listPosition).getId()+"");
                startActivity(PrescriptionMouldDetailsActivity.class,bundle);

            }
        });

        // 开启自动加载功能
        refreshLayout = findViewById(R.id.refreshLayout);
        refreshLayout.setEnableAutoLoadMore(true);
        refreshLayout.setOnRefreshListener(refreshLayout -> {

            getMyRecipe();

        });

        refreshLayout.setOnLoadMoreListener(refreshLayout -> {

            refreshLayout.finishLoadMore(true);

        });

        //触发自动刷新
        refreshLayout.autoRefresh();



    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what){

                case 1:

                    try {

                        lists.clear();

                        JSONObject object = new JSONObject(msg.obj.toString());
                        Gson gson = new Gson();
                        if (object.getInt("code") == 1){

                            JSONArray array = object.getJSONArray("data");

                            for (int i =0 ;i<array.length();i++){

                                MyRecipe myRecipe = gson.fromJson(array.get(i).toString(),MyRecipe.class);
                                lists.add(myRecipe);

                            }

                            adapter.setListAndNotifyDataSetChanged(lists);

                        }
                        if (lists.size() > 0) {

                            mEmptyLayout.setVisibility(View.GONE);

                        } else {

                            mEmptyLayout.setVisibility(View.VISIBLE);

                        }

                        refreshLayout.finishRefresh(true);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

            }

        }
    };

    public void getMyRecipe(){

        List<String> ls = new ArrayList<>();
        ls.add("10");
        ls.add(MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        ls.add("1");
        //请求网络
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("sign", AllUtil.md5(sing(ls)));
        params.put("token", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.token, ""));
        params.put("uid", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("ts", System.currentTimeMillis() / 1000); // 当前时间戳
        params.put("cmd", "getMyRecipe");
        params.put("data[patient_id]", MySharedPreferences.getInstance(mActivity).getString(MySharedPreferences.uid, ""));
        params.put("data[row]", "1");
        params.put("data[number]", "10");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/app/", params, handler, 1, 404);

    }

}
